<?php
	/**
	 * Salt-api整合类
	 */
	class Salt{
		/* 变量 */
		private $api = "https://1.1.1.60:8001"; //salt-api地址
		private $user = "saltapi"; //salt-api用户
		private $pwd = "123456"; //salt-api密码
  		
  		/* 私有类方法 */
  		private function get_token(){
			$url=$this->api;
			$username=$this->user;
			$password=$this->pwd;
			$data = array("username"=>$username,"password"=>$password,"eauth"=>"pam");
			$headerArray =array("Accept:application/json");
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url.'/login');
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$token = $output['return'][0]['token'];
        	return $token;
  		}
  		
  		/* 通过jid获取执行结果,入参$jobId字符串类型 */
  		public function look_job($jobId){
  			$url=$this->api;
  			$token = $this->get_token();
  			$data = array("client"=>"runner","fun"=>"jobs.lookup_jid","jid"=>$jobId);
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	return $output;
  		}
  		
  		/* 获取包括认证、未认证salt主机 */
  		public function get_key_list(){
  			$url=$this->api;
  			$token = $this->get_token();
  			$data = array("client"=>"wheel","fun"=>"key.list_all");
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobdata = $output['return'][0]['data']['return'];
        	return $jobdata;
  		}
		
		/* 接受salt主机key，入参$hostId字符串类型 */
  		public function acc_key($hostId){
  			$url=$this->api;
  			$token = $this->get_token();
  			$data = array("client"=>"wheel","fun"=>"key.accept","match"=>$hostId);
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobdata = $output['return'][0]['data']['return'];
        	return $jobdata;
  		}
  		
  		/* 拒绝salt主机key，入参$hostId字符串类型  */
  		public function del_key($hostId){
  			$url=$this->api;
  			$token = $this->get_token();
  			$data = array("client"=>"wheel","fun"=>"key.delete","match"=>$hostId);
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobdata = $output['return'][0]['data']['return'];
        	return $jobdata;
  		}
  		
  		/* 获取运行中的任务 */
  		public function get_runed_servers(){
  			$url=$this->api;
  			$token = $this->get_token();
  			$data = array("client"=>"runner","fun"=>"jobs.active");
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobdata = $output['return'][0]['data']['return'];
        	return $jobdata;
  		}
  		
  		/* 主机存活检测，入参$hostId数组、字符串类型 */
  		public function check_host($hostId="*"){
  			$url=$this->api;
  			$token = $this->get_token();
  			$TGT="";
  			if(is_array($hostId)){
  				for($i=0;$i<count($hostId);$i++){
  					$TGT .=$hostId[$i].',';
  				}
  				$TGT = substr($TGT, 0, -1);
  				$data = array("client"=>"local","fun"=>"test.ping","tgt"=>$TGT,"expr_form"=>"list");
  			}else{
  				$TGT = $hostId;
  				$data = array("client"=>"local","fun"=>"test.ping","tgt"=>$TGT);
  			}
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobdata = $output;
        	return $jobdata;
  		}
  		
  		/* 远程异步执行模块 */
  		public function run_salt($arg,$hostId="*"){
  			$url=$this->api;
  			$token = $this->get_token();
  			$TGT="";
  			if(is_array($hostId)){
  				for($i=0;$i<count($hostId);$i++){
  					$TGT .=$hostId[$i].',';
  				}
  				$TGT = substr($TGT, 0, -1);
  				$data = array("client"=>"local_async","fun"=>"cmd.run","tgt"=>$TGT,"expr_form"=>"list","arg"=>$arg);
  			}else{
  				$TGT = $hostId;
  				$data = array("client"=>"local_async","fun"=>"cmd.run","tgt"=>$TGT,"arg"=>$arg);
  			}
  			
  			$headerArray =array("Accept:application/json","X-Auth-Token:".$token);
        	$ch = curl_init();
       		curl_setopt($ch, CURLOPT_URL, $url);
        	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        	curl_setopt($ch, CURLOPT_POST, 1);
        	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_HTTPHEADER,$headerArray);
        	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
        	$output = curl_exec($ch);
        	curl_close($ch);
        	$output = json_decode($output,true);
        	$jobId = $output['return'][0];
        	return $jobId;
  		}
  			
	}
?>